//
//  WLDashBorderView.swift
//  WLVom
//
//  Created by Xiaochao Su 苏小超 on 2018/3/6.
//  Copyright © 2018年 Yongpeng.Zhu. All rights reserved.
//

import UIKit
import SnapKit

class WLDashBorderView:UIView{
    var lastContentLabel:UILabel?
    var borderLayer :CAShapeLayer?
    var viewHeight : CGFloat = 20
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    
    /// 添加虚线边框
    func dispalyDashBorderLayout(){
        self.layoutIfNeeded()
        borderLayer?.removeFromSuperlayer()
        borderLayer = CAShapeLayer()
        borderLayer?.strokeColor = UIColor(red: 229.0/255, green: 229.0/255, blue: 229.0/255, alpha: 1).cgColor
        borderLayer?.lineDashPattern = [5, 5]
        borderLayer?.frame = self.bounds
//        print("borderLayer?.frame\(borderLayer?.frame)")
        borderLayer?.fillColor = nil
        borderLayer?.path = UIBezierPath(rect: self.bounds).cgPath
        if let b = borderLayer{
             self.layer.addSublayer(b)
        }
       
    }
    
    func removeAllSubViews(){
        for v in self.subviews{
            v.removeFromSuperview()
        }
        lastContentLabel = nil
        viewHeight = 20
    }
    
    override func layoutSubviews() {
        dispalyDashBorderLayout()
    }
    
    
    
    /// 添加title和文字
    ///
    /// - Parameters:
    ///   - title: *
    ///   - content: 文字描述
    func addLine(title:String,content:String,bottom:CGFloat = 0,titleColor:UIColor? = UIColor.black){
        let titleLabel = UILabel()
        titleLabel.translatesAutoresizingMaskIntoConstraints = false
        titleLabel.text = title
        titleLabel.font = UIFont.PingFangSC(font: .light, ofSize: 12)
        titleLabel.textColor = titleColor != nil ? titleColor:UIColor.black
        self.addSubview(titleLabel)
        
        titleLabel.snp.makeConstraints { (make) in
            if let l = lastContentLabel{
                make.left.equalToSuperview().offset(10)
                make.top.equalTo(l.snp.bottom).offset(2)
            }else{
                make.left.equalToSuperview().offset(10)
                make.top.equalToSuperview().offset(10)
            }
        }
        
        let contentLabel = UILabel()
        contentLabel.translatesAutoresizingMaskIntoConstraints = false
        contentLabel.lineBreakMode = .byCharWrapping
        contentLabel.numberOfLines = 0
        contentLabel.text = content
        contentLabel.font = UIFont.PingFangSC(font: .light, ofSize: 12)
        contentLabel.textColor = titleColor != nil ? titleColor:UIColor.black
        self.addSubview(contentLabel)
        
        contentLabel.snp.makeConstraints { (make) in
            if let l = lastContentLabel {
                make.left.equalToSuperview().offset(35)
                make.top.equalTo(l.snp.bottom).offset(2)
                make.right.equalToSuperview().offset(-10)
            }else{
                make.left.equalToSuperview().offset(35)
                make.top.equalToSuperview().offset(10)
                make.right.equalToSuperview().offset(-10)
            }
            if bottom > 0{
                make.bottom.equalToSuperview().offset(-bottom)
            }
            
        }
        contentLabel.layoutIfNeeded()
        self.layoutIfNeeded()
        
        lastContentLabel = contentLabel
        viewHeight += content.height(withConstrainedWidth: UIScreen.main.bounds.size.width * 325/375 - 45, font: contentLabel.font)
        
    }
}
